package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GweoT;
import cn.e4j.modular.crm.mapper.Gwei009Mapper;
import cn.e4j.modular.crm.service.Gwei009Service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-08-09
 */
@Service
public class Gwei009ServiceImpl extends ServiceImpl<Gwei009Mapper, GweoT> implements Gwei009Service {

    @Override
    @Transactional(readOnly = true)
    public List<Dto> getList(Dto inDto, Page<Object> page) {
//        return this.baseMapper.getList(inDto,page);
        return InvokeUtil.invokeBeanMethod("gwei009Mapper,getList",inDto,page);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Dto> list(Dto inDto) {
        return this.baseMapper.list(inDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Dto getRate(Dto inDto) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ent",shiroUser.getDBC());
        if(inDto.getString("CURRENCY").equals("RMB")){
            outDto.put("RATE","1");
            return outDto;
        }
        String gweodocdate = inDto.getString("GWEODOCDATE");
        if(AppCxt.getParam("change_rate_type_").equals( "1")){//日汇率
            try {
                Date gweodocdateNew = new SimpleDateFormat("yyyy-MM-dd").parse(gweodocdate);
                inDto.put("CURRENT_TIME",new SimpleDateFormat("yyyy-MM-dd").format(gweodocdateNew));
                Dto gwelDto = this.baseMapper.getGwel005(inDto);
                if(gwelDto != null && gwelDto.size() > 0){
                    outDto.put("RATE",gwelDto.getString("GWEL005"));
                }else {
                    outDto.put("RATE","N");
                }
            }catch (ParseException e) {
                e.printStackTrace();
            }
        } else {//月汇率
            try {
                Date gweodocdateNew = new SimpleDateFormat("yyyy-MM-dd").parse(gweodocdate);
                inDto.put("CURRENT_TIME",new SimpleDateFormat("yyyy-MM-dd").format(gweodocdateNew));
                Dto gwekDto = this.baseMapper.getGwek005(inDto);
                if(gwekDto != null && gwekDto.size() > 0){
                    outDto.put("RATE",gwekDto.getString("GWEK005"));
                }else {
                    outDto.put("RATE","N");
                }
            }catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return outDto;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Dto> listOrg_1(Dto inDto) {
//        return this.baseMapper.listOrg_1(inDto);
        return InvokeUtil.invokeBeanMethod("gwei009Mapper,listOrg_1",inDto);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Dto> listGweo(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ent", shiroUser.getDBC());
        List<Dto> resultDto = this.baseMapper.listGweo(inDto);
        for (int i = 0; i < resultDto.size(); i++) {
            resultDto.get(i).put("sys_rn_", i + 1);
        }
        return resultDto;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Dto> getOebPage(Dto inDto, Page<Object> page) {
        return this.baseMapper.getOebPage(inDto,page);
    }

    @Transactional
    public void add(List<GweoT> gweoTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //增加新的明细
        for (int i = 0; i < gweoTList.size(); i++) {
            GweoT gweo = gweoTList.get(i);
            gweo.setGWEOENT(shiroUser.getDBC());
            gweo.setGWEOACTI("N");
            gweo.setGWEOLEGAL(gweo.getGWEOPLANT());
            gweo.setGWEOUSER(shiroUser.getACCOUNT());
            gweo.setGWEOGRUP(shiroUser.getORG_ID());
            gweo.setGWEODATE(LocalDate.now());
            gweo.setGWEOTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gweo.setGWEOORIG(shiroUser.getORG_ID());
            gweo.setGWEOORIU(shiroUser.getACCOUNT());
            gweo.insert();
        }
    }

    @Transactional
    public void edit(List<GweoT> gweoTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //删除旧的
        UpdateWrapper<GweoT> gweoTUpdateWrapper=new UpdateWrapper<>();
        gweoTUpdateWrapper.eq("GWEOENT",shiroUser.getDBC());
        gweoTUpdateWrapper.eq("GWEODOCNO",gweoTList.get(0).getGWEODOCNO());
        int count = this.baseMapper.delete(gweoTUpdateWrapper);

        //增加新的
        for (int i = 0; i < gweoTList.size(); i++) {
            GweoT gweo = gweoTList.get(i);
            gweo.setGWEOENT(shiroUser.getDBC());
            gweo.setGWEOACTI("N");
            gweo.setGWEOLEGAL(gweo.getGWEOPLANT());
            gweo.setGWEOUSER(shiroUser.getACCOUNT());
            gweo.setGWEOGRUP(shiroUser.getORG_ID());
            gweo.setGWEODATE(LocalDate.now());
            gweo.setGWEOTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
            gweo.setGWEOORIG(shiroUser.getORG_ID());
            gweo.setGWEOORIU(shiroUser.getACCOUNT());

            gweo.insert();
        }
    }

    @Transactional
    @Override
    public void delRow(GweoT gweoT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        UpdateWrapper<GweoT> gweoTUpdateWrapper = new UpdateWrapper<>();
        gweoTUpdateWrapper.eq("GWEOENT", shiroUser.getDBC());
        gweoTUpdateWrapper.eq("GWEODOCNO",gweoT.getGWEODOCNO());
        gweoTUpdateWrapper.eq("GWEOSEQ", gweoT.getGWEOSEQ());
        gweoT.delete(gweoTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<GweoT> gweoTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        for (GweoT gweoT : gweoTList) {
            UpdateWrapper<GweoT> gweoTUpdateWrapper = new UpdateWrapper<>();
            gweoTUpdateWrapper.eq("GWEOENT", shiroUser.getDBC());
            gweoTUpdateWrapper.eq("GWEODOCNO",gweoT.getGWEODOCNO());
            gweoTUpdateWrapper.eq("GWEOSEQ", gweoT.getGWEOSEQ());
            gweoT.delete(gweoTUpdateWrapper);
        }
    }

    @Transactional
    @Override
    public Dto confirm(List<GweoT> gweoTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.setAppCode(200);
        for (GweoT gweoT : gweoTList) {
            UpdateWrapper<GweoT> gweoTUpdateWrapper = new UpdateWrapper<>();
            gweoTUpdateWrapper.eq("GWEOENT", shiroUser.getDBC());
            gweoTUpdateWrapper.eq("GWEODOCNO",gweoT.getGWEODOCNO());
//            gweoTUpdateWrapper.eq("GWEOSEQ", gweoT.getGWEOSEQ());//去掉项次，整单确认
            gweoT.setGWEOACTI("Y");
            if(!gweoT.update(gweoTUpdateWrapper)){
                outDto.setAppCode(400);
                outDto.setAppMsg("确认失败");
                //手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                break;
            }
        }
        return outDto;
    }

    @Transactional
    @Override
    public Dto unConfirm(List<GweoT> gweoTList) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.setAppCode(200);
        for (GweoT gweoT : gweoTList) {
            UpdateWrapper<GweoT> gweoTUpdateWrapper = new UpdateWrapper<>();
            gweoTUpdateWrapper.eq("GWEOENT", shiroUser.getDBC());
            gweoTUpdateWrapper.eq("GWEODOCNO",gweoT.getGWEODOCNO());
//            gweoTUpdateWrapper.eq("GWEOSEQ", gweoT.getGWEOSEQ());//去掉项次，整单取消确认
            gweoT.setGWEOACTI("N");
            if(!gweoT.update(gweoTUpdateWrapper)){
                outDto.setAppCode(400);
                outDto.setAppMsg("确认失败");
                //手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                break;
            }
        }
        return outDto;
    }



}
